package com.splant.android.smart.CommunicationLevel;

import android.content.Context;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;
import com.splant.android.smart.Global;
import com.splant.android.smart.ModelLevel.sPlantRequestParams;
import org.apache.http.entity.StringEntity;

public class sPlantAsyncHttpClient extends AsyncHttpClient {

    private final String SERVER_URL = "http://120.77.53.132:3001/api/v2.1/";
    public final static String USER_LOGIN = "userLogin";
    public final static String SWITCH_ALLDEVICES = "switchAllDevices";
    public final static String GETAREALIST="getAreaList";
    public final static String GETWATERDEVICES = "getWaterDevices";
    public final static String GETDETECTORDEVICES= "getDetectorDevices";
    public final static String ADD_AUTOWATER= "addAutoWater";
    public final static String UPDATE_AUTOWATER= "updateAutoWater";
    public final static String ADD_TIMERWATER= "addTimerWater";
    public final static String UPDATE_TIMERWATER= "updateTimerWater";
    public final static String SETWATER= "setWaters";
    public final static String GETWATERDETAILS= "getWaterDetails";
    public final static String GETDETECTORDATAS= "getDetectorDatas";
    public final static String GETLATELYWATERDATAS= "getLatelyWaterDatas";
    public final static String GETWATERDATAS= "getWaterDatas";
    public final static String GETAREADEVICES= "getAreaDevices";
    public final static String GETSEARCHDEVICES= "getSearchDevices";
    public final static String GETWEATHERCONDITION= "getWeatherCondition";
    public final static String GETGATEWAYS= "getGateways";
    public final static String GETPLANTTYPES= "getPlantTypes";
    public final static String GETPLANTLIST= "getPlantList";
    public final static String GETPLANT= "getPlant";
    public final static String UPDATEPSW= "updatePsw";
    public final static String GETMESSAGELIST= "getMessageList";
    public final static String GETDAYTIMERWATERS="getDayTimerWaters";
    public final static String GETMETEOROLOGICALINFO="getMeteorologicalInfo";
    public final static String GETMETEOROLOGICALDEVICES="getMeteorologicalDevices";

    public final static String DEVICE_WATERING_STATUS="DeviceWateringStatus";


    private AsyncHttpClient client;
    private static volatile sPlantAsyncHttpClient sPlantClient=null;

    public sPlantAsyncHttpClient() {
         client = new AsyncHttpClient();
    }

    public static sPlantAsyncHttpClient getInstance() {
          synchronized (sPlantAsyncHttpClient.class){
              if (sPlantClient==null)
                  sPlantClient=new sPlantAsyncHttpClient();
              sPlantClient.setTimeout(30000);
              sPlantClient.setConnectTimeout(30000);
          }
        return sPlantClient;
    }

    public RequestHandle post(Context context, String api, sPlantRequestParams params, ResponseHandlerInterface responseHandler) {

        String postUrl= SERVER_URL+api;
        String contentType="application/json";
        StringEntity stringEntity = null;
        try
        {
            params.putParams("from",2);

            if (Global.user!=null)
                params.putParams("token",Global.user.getToken());

            stringEntity = new StringEntity(params.toString());
        }
        catch (Exception e) {
            e.printStackTrace();
        }

       return super.post(context,postUrl,stringEntity,contentType,responseHandler);
    };

    @Override
    public RequestHandle get(String api, RequestParams params,ResponseHandlerInterface responseHandler) {

            String url= SERVER_URL+api;
            params.put("from",2);

            if (Global.user!=null)
                params.put("token",Global.user.getToken());

            return super.get(url,params,responseHandler);

    }

}
